/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.msg.entity;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.directwebremoting.annotations.DataTransferObject;
import org.directwebremoting.annotations.RemoteProperty;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import cn.net.ecode.common.mapper.JsonMapper;
import cn.net.ecode.common.persistence.DataEntity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 消息管理Entity
 * @author ThinkGem
 * @version 2015-5-30
 */
@DataTransferObject
public class Message extends DataEntity<Message> {
	
	private static final long serialVersionUID = 1L;
	
	@RemoteProperty
	private String type;			// 消息类型(1通知 2消息 3短信 4邮件)
	
	@RemoteProperty
	private String contentTitle;	// 内容标题
	private String contentLevel;	// 内容级别(1普通 2一般 3紧急)
	private String contentType;		// 内容类型(1公告 2新闻 3会议 4其它)
	@RemoteProperty
	private String contentText;		// 消息内容
	
	private String receiverType;	// 接受者类型（1用户 2部门 3角色 4岗位）
	private String receiverCodes;	// 接受者字符串
	private String receiverNames;	// 接受者名称字符串
	
	@RemoteProperty
	private String sender;			// 发送者
	@RemoteProperty
	private String senderName;		// 发送者姓名
	@RemoteProperty
	private Date sendDate;			// 发送时间

	@RemoteProperty
	private String bizKey;			// 业务主键(关联业务表)
	@RemoteProperty
	private String buttons;			// 消息中的按钮JSON（button: [{name:'',href:'',onclick:''}]）
	
	private Date beginSendDate;		// 开始 发送时间
	private Date endSendDate;		// 结束 发送时间
	
	private MsgStatis msgStatis;   	// 消息统计
	private String readStatus;		// 读取状态（0未送达 1未读 2已读） msg_message_record
	
	private List<MessageRecord> messageRecordList;//消息记录
	
	private String[] codes; // 查询数据参数用
	
	public Message() {
		super();
		//this.contentLevel = "1";
	}

	public Message(String id){
		super();
		this.id = id;
	}

	public Message(String[] codes){
		super();
		this.codes = codes;
	}

	@NotBlank(message="消息类型不能为空")
	@Length(min=0, max=1, message="消息类型长度不能超过 1 个字符")
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	@NotBlank(message="内容标题不能为空")
	@Length(min=0, max=200, message="内容标题长度不能超过 200 个字符")
	public String getContentTitle() {
		return contentTitle;
	}

	public void setContentTitle(String contentTitle) {
		this.contentTitle = contentTitle;
	}
	
	@NotBlank(message="内容级别不能为空")
	@Length(min=0, max=1, message="内容级别长度不能超过 1 个字符")
	public String getContentLevel() {
		return contentLevel;
	}

	public void setContentLevel(String contentLevel) {
		this.contentLevel = contentLevel;
	}
	
	@Length(min=0, max=1, message="内容类型长度不能超过 1 个字符")
	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	
	@NotBlank(message="消息内容不能为空")
	@Length(min=0, max=2000, message="消息内容长度不能超过 2000 个字符")
	public String getContentText() {
		return contentText;
	}

	public void setContentText(String contentText) {
		this.contentText = contentText;
	}
	
	@NotBlank(message="接受者类型不能为空")
	@Length(min=0, max=1, message="接受者类型长度不能超过 1 个字符")
	public String getReceiverType() {
		return receiverType;
	}

	public void setReceiverType(String receiverType) {
		this.receiverType = receiverType;
	}
	
	@NotBlank(message="接受者字符串不能为空")
	@Length(min=0, max=3000, message="接受者字符串长度不能超过 3000 个字符")
	public String getReceiverCodes() {
		return receiverCodes;
	}

	public void setReceiverCodes(String receiverCodes) {
		this.receiverCodes = receiverCodes;
	}
	
	@NotBlank(message="接受者名称不能为空")
	@Length(min=0, max=2000, message="接受者名称字符串长度不能超过 2000 个字符")
	public String getReceiverNames() {
		return receiverNames;
	}

	public void setReceiverNames(String receiverNames) {
		this.receiverNames = receiverNames;
	}
	
	@Length(min=0, max=64, message="发送者长度不能超过 64 个字符")
	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}
	
	@Length(min=0, max=100, message="发送者姓名长度不能超过 100 个字符")
	public String getSenderName() {
		return senderName;
	}

	public void setSenderName(String senderName) {
		this.senderName = senderName;
	}
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getSendDate() {
		return sendDate;
	}

	public void setSendDate(Date sendDate) {
		this.sendDate = sendDate;
	}
	
	@Length(min=0, max=64, message="业务主键长度不能超过 64 个字符")
	public String getBizKey() {
		return bizKey;
	}

	public void setBizKey(String bizKey) {
		this.bizKey = bizKey;
	}
	
	public String getButtons() {
		return buttons;
	}

	public void setButtons(String buttons) {
		this.buttons = buttons;
	}

	public Date getBeginSendDate() {
		return beginSendDate;
	}

	public void setBeginSendDate(Date beginSendDate) {
		this.beginSendDate = beginSendDate;
	}
	
	public Date getEndSendDate() {
		return endSendDate;
	}

	public void setEndSendDate(Date endSendDate) {
		this.endSendDate = endSendDate;
	}

	public List<MessageRecord> getMessageRecordList() {
		return messageRecordList;
	}

	public void setMessageRecordList(List<MessageRecord> messageRecordList) {
		this.messageRecordList = messageRecordList;
	}

	public MsgStatis getMsgStatis() {
		return msgStatis;
	}

	public void setMsgStatis(MsgStatis msgStatis) {
		this.msgStatis = msgStatis;
	}

	public String getReadStatus() {
		return readStatus;
	}

	public void setReadStatus(String readStatus) {
		this.readStatus = readStatus;
	}
	
	/**
	 * 获取按钮列表
	 * @return
	 */
	public List<Map<String, String>> getButtonList(){
		List<Map<String, String>> list = Lists.newArrayList();
		if (buttons == null){
			buttons = "[]";
		}
		list = JsonMapper.fromJsonString(buttons, List.class);
		return list;
	}
	
	/**
	 * 添加按钮
	 * @param values new String[]{"name", "href", "onclick"}
	 */
	public void addButton(String... values){
		if (values == null){
			return;
		}
		List<Map<String, String>> list = getButtonList();
		Map<String, String> map = Maps.newHashMap();
		for (int i = 0; i < values.length; i++){
			switch (i) {
			case 0:
				map.put("name", values[i]);
				break;
			case 1:
				map.put("href", values[i]);
				break;
			case 2:
				map.put("onclick", values[i]);
				break;
			default:
				break;
			}
		}
		list.add(map);
		buttons = JsonMapper.toJsonString(list);
	}

	public String[] getCodes() {
		return codes;
	}

	public void setCodes(String[] codes) {
		this.codes = codes;
	}

	/**
	 * 消息类型(1通知 2消息 3短信 4邮件 5微信)，1通知和2消息为内部消息
	 */
	public static final String TYPE_NOTIFY = "1";
	public static final String TYPE_MESSAGE = "2";
	public static final String TYPE_SMS = "3";
	public static final String TYPE_EMAIL = "4";
	public static final String TYPE_WEIXIN = "5";
	
	/**
	 * 接受者类型（0未配置，设置什么使用什么（ 可为手机号、邮箱、微信 ） 1用户 2部门 3角色 4岗位  all_user所有用户  ）
	 */
	public static final String RECEIVER_TYPE_NONE = "0";
	public static final String RECEIVER_TYPE_USER = "1";
	public static final String RECEIVER_TYPE_OFFICE = "2";
	public static final String RECEIVER_TYPE_ROLE = "3"; 
	public static final String RECEIVER_TYPE_POST = "4";
	public static final String RECEIVER_ALL_USER = "all_user";
	
	/**
	 * 状态标记（0：发送 6：接收 9 草稿）
	 */
	public static final String STATUS_RECEIVER = "6";
}